package com.ruoyi.common.opio.util;

import com.magus.OPException;
import com.magus.jdbc.OPResultSet;
import com.magus.jdbc.net.OPConf;
import com.magus.opio.net.IOPConnect;
import com.magus.opio.pool.OPConnectManage;
import com.ruoyi.common.config.OpcConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.concurrent.*;

@Configuration
public class OPPool {
	// 创建连接池
	private static OPConnectManage manage;

	private static OPPool opPool;

	@Resource
	private OpcConfig opcConfig;

	@PostConstruct
	public void init() {
		opPool = this;
		opPool.opcConfig = this.opcConfig;
		// 连接池配置信息
		OPConf config = new OPConf(opcConfig.getIp(), Integer.valueOf(opcConfig.getPort()), opcConfig.getUserName(), opcConfig.getPassword(), true, 1800);
		// 实例化连接池
		manage = OPConnectManage.getInstance(config, 30, 2);
	}

	/*
	 * 从连接池获取连接
	 */
	public synchronized static IOPConnect getConn() {
//		IOPConnect conn = null;
//		ThreadPoolExecutor exec = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
//		Callable<IOPConnect> doJob = new Callable<IOPConnect>() {
//			@Override
//			public IOPConnect call() throws Exception {
//				//开始执⾏耗时操作
//				IOPConnect conn = null;
//				try {
//					if (manage == null) {
//						return conn;
//					}
//					conn = manage.getConnect();
//				} catch (OPException e) {
//					// TODO Auto-generated catch block
//					System.out.println("获取连接出错！");
//					e.printStackTrace();
//				}
//				return conn;
//			}
//		};
//		try {
//			Future<IOPConnect> future = exec.submit(doJob);
//			IOPConnect obj = future.get(1000 * 2, TimeUnit.MILLISECONDS); //任务处理超时时间设为 1 秒
//			System.out.println("任务成功返回:" + obj);
//			conn = obj;
//		} catch (TimeoutException ex) {
//			System.out.println("处理超时啦....");
//		} catch (Exception e) {
//			System.out.println("处理失败.");
//		}
//		// 关闭线程池
//		exec.shutdown();
		IOPConnect conn = null;
		try {
			conn = manage.getConnect();
		} catch (OPException e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			System.out.println("获取连接出错！");
		} finally {
			return conn;
		}
	}

	public static Future<IOPConnect> submitFun() throws InterruptedException {
		//业务逻辑具体代码
		IOPConnect conn = getConn();
		return new AsyncResult<IOPConnect>(conn);
	}

	/**
	 * 释放连接
	 * 
	 * @param conn
	 */
	public synchronized static void freeConn(IOPConnect conn) {

		try {
			manage.freeConnect(conn);
		} catch (OPException e) {
			// TODO Auto-generated catch block
			System.out.println("释放连接出错!");
			e.printStackTrace();
		}
	}

	/**
	 * 关闭结果集，并释放连接
	 * 
	 * @param conn
	 * @param rs
	 */
	public synchronized static void close(IOPConnect conn, OPResultSet rs) {

		try {
			if (null != rs) {
				rs.close();
			}
			manage.freeConnect(conn);
			System.out.println("关闭数据库连接成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("关闭ResultSet失败！");
			e.printStackTrace();
		} catch (OPException e) {
			// TODO Auto-generated catch block
			System.out.println("连接释放失败！");
			e.printStackTrace();
		}

	}
}
